package zy.wq.security.bean;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.MappedSuperclass;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Version;


@MappedSuperclass
public abstract class AbstractModel implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 4695617538162217920L;

	@Version
	@Column(name="VERSION")
	private long version;
	
	@Column(name="CREATE_BY_USER_ID",nullable=true,length=50)
	private String createByUserId;
	@Column(name="CREATE_BY_USER_NAME",nullable=true,length=50)
	private String createByUserName;
	@Column(name="CREATE_TIME",nullable=true)
	private Date createTime;
	
	@Column(name="LAST_UPDATE_BY_USER_ID",nullable=true,length=50)
	private String lastUpdateByUserId;
	@Column(name="LAST_UPDATE_BY_USER_NAME",nullable=true,length=50)
	private String lastUpdateByUserName;
	@Column(name="LAST_UPDATE_TIME",nullable=true)
	private Date lastUpdateTime;
	
	public long getVersion() {
		return version;
	}

	public void setVersion(long version) {
		this.version = version;
	}

	public String getCreateByUserId() {
		return createByUserId;
	}

	public void setCreateByUserId(String createByUserId) {
		this.createByUserId = createByUserId;
	}

	public String getCreateByUserName() {
		return createByUserName;
	}

	public void setCreateByUserName(String createByUserName) {
		this.createByUserName = createByUserName;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public String getLastUpdateByUserId() {
		return lastUpdateByUserId;
	}

	public void setLastUpdateByUserId(String lastUpdateByUserId) {
		this.lastUpdateByUserId = lastUpdateByUserId;
	}

	public String getLastUpdateByUserName() {
		return lastUpdateByUserName;
	}

	public void setLastUpdateByUserName(String lastUpdateByUserName) {
		this.lastUpdateByUserName = lastUpdateByUserName;
	}

	public Date getLastUpdateTime() {
		return lastUpdateTime;
	}

	public void setLastUpdateTime(Date lastUpdateTime) {
		this.lastUpdateTime = lastUpdateTime;
	}

	@PrePersist
	public void prePersist() {
		User user = AppSecurityUtils.currentUser();
		if(user != null) {
			this.setCreateByUserId(user.getId());
			this.setCreateByUserName(user.getName());
			this.setLastUpdateByUserId(user.getId());
			this.setLastUpdateByUserName(user.getName());
		}
		if(this.getCreateTime() == null) {
			Date currentTime = new Date();
			this.setCreateTime(currentTime);
		}
		this.setLastUpdateTime(this.getCreateTime());
		
	}

	@PreUpdate
	public void preUpdate() {
		User user = AppSecurityUtils.currentUser();
		if(user != null) {
			this.setLastUpdateByUserId(user.getId());
			this.setLastUpdateByUserName(user.getName());
		}
		this.setLastUpdateTime(new Date());
	}
	
}
